//
//  HUDDialog.m
//  storeapp
//
//  Created by huangyi on 13-3-12.
//  Copyright (c) 2013年 ios. All rights reserved.
//

#import "HudDialog.h"

@interface HudDialog ()

@end

@implementation HudDialog

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

+(HudDialog*)hudWithText:(NSString*)text{
    dispatch_block_t ok=^{};
    NSArray *button=[NSArray arrayWithObjects:LOC_STR(@"继续"), nil];
    NSArray *action=[NSArray arrayWithObjects:ok, nil];
    return [HudDialog hudWithText:text button:button action:action];
}
+(HudDialog*)hudWithText:(NSString *)text button:(NSArray*)button action:(NSArray*)action{
    HudDialog *hud=[[HudDialog alloc] init];
    hud.textString=text;
    hud.button=button;
    hud.action=action;
    return hud;
}

-(void)buildHud{
    CGFloat width=120;
    CGFloat spacing=10;
    CGFloat radius=6;
    UIFont *font=UIFontFromString(@"@16");
    CGSize button=CGSizeMake(80, 30);

    UIView *container=[[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:container];
    self.container=container;
    container.clipsToBounds=YES;
    container.backgroundColor=[UIColor colorWithWhite:0.9 alpha:1];
    container.layer.cornerRadius=radius;
    container.layer.shadowRadius=3;
    container.layer.shadowOpacity=1;
    container.layer.shadowOffset=CGSizeZero;
    container.layer.shadowColor=[UIColor blackColor].CGColor;
    container.autoresizingMask=(UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin);

    UILabel *textView=[[UILabel alloc] initWithFrame:CGRectZero];
    [container addSubview:textView];
    if (self.textString) {
        textView.text=self.textString;
        textView.font=font;
        textView.numberOfLines=0;
        textView.backgroundColor=[UIColor clearColor];
        [textView sizeToFit];
    }
    width=MAX(width, CGRectGetWidth(textView.frame));

    UIView *buttonView=[[UIView alloc] initWithFrame:CGRectZero];
    [container addSubview:buttonView];
    for (int i=0; i<self.button.count; i++) {
        UIButton *buttonObj=[[UIButton alloc] initWithFrame:CGRectZero];
        [buttonView addSubview:buttonObj];
        buttonObj.frame=CGRectMake((button.width+spacing)*i, 0, button.width, button.height);
        [buttonObj setTitle:self.button[i] forState:UIControlStateNormal];
        [buttonObj setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [buttonObj setBackgroundColor:[UIColor whiteColor]];
        buttonObj.titleLabel.font=font;
        buttonObj.layer.cornerRadius=radius;
        buttonObj.tag=i;
        [buttonObj addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        buttonView.frame=CGRectMake(0, 0, CGRectGetMaxX(buttonObj.frame), CGRectGetMaxY(buttonObj.frame));
    }
    [buttonView sizeToFit];
    width=MAX(width, CGRectGetWidth(buttonView.frame));

    width=spacing+width+spacing;
    textView.frame=CGRectOffset(textView.frame, 0, spacing);
    textView.center=CGPointMake(width/2, textView.center.y);
    buttonView.frame=CGRectOffset(buttonView.frame, 0, CGRectGetMaxY(textView.frame)+spacing);
    buttonView.center=CGPointMake(width/2, buttonView.center.y);
    container.frame=CGRectMake(0, 0, width, CGRectGetMaxY(buttonView.frame)+spacing);
    container.center=CGPointMake(CGRectGetWidth(self.view.frame)/2, CGRectGetHeight(self.view.frame)/2);
}

-(void)show{
    [super show];
}
-(void)hide{
    [super hide];
}

-(void)buttonClicked:(id)sender{
    UIButton *button=sender;
    dispatch_block_t code=self.action[button.tag];
    if (code) {
        code();
    }
    [self hide];
}
@end
